package com.calsoft.Hops_Mobile_Tablet;
/*
 * User: Minh Diep
 * Date: 7/9/13
 * Time: 3:29 PM
 */

import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import com.calsoft.Hops_Mobile_Tablet.base.App;
import com.calsoft.Hops_Mobile_Tablet.base.OpToursItemAdapter;
import com.calsoft.Hops_Mobile_Tablet.base.OpToursItemData;
import com.calsoft.Hops_Mobile_Tablet.component.SwipeDismissListViewTouchListener;
import com.calsoft.Hops_Mobile_Tablet.sql.OpToursDbAdapter;
import com.calsoft.Hops_Mobile_Tablet.util.SharedPrefs;

public class WishListFragment extends Fragment {

    private OpToursDbAdapter mDbHelper;
    private OpToursItemAdapter opToursItemAdapter;
    private SharedPrefs sPrefs;
    private ListView listView;
    private TextView emptyWishlistTv;

    public static final String WISH_LIST_LAND_SERVICE_CODE = "com.calsoft.hops.mobile.wish.list.land.service.code";


    public WishListFragment() {
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_wish_list, container, false);
        getActivity().setTitle("Wish List");

        listView = (ListView) rootView.findViewById(R.id.service_list);
        emptyWishlistTv = (TextView) rootView.findViewById(R.id.wishlist_empty_tv);

        //Open our database
        mDbHelper = new OpToursDbAdapter(getActivity());
        return rootView;
    }

    private void setupData() {
        opToursItemAdapter = null;
        opToursItemAdapter = new OpToursItemAdapter(getActivity().getApplicationContext());
        Cursor cursor = mDbHelper.fetchServiceByWishList();
        cursor.moveToFirst();
        while (!cursor.isAfterLast()){
            OpToursItemData item = new OpToursItemData(cursor);
            opToursItemAdapter.addItem(item);
            cursor.moveToNext();
        }
        cursor.close();
    }

    @Override
    public void onResume() {
        super.onResume();
        mDbHelper.open();
        setupData();
        displayOpToursListView();
    }

    private void displayOpToursListView() {
        if (opToursItemAdapter.getCount() == 0){
            emptyWishlistTv.setVisibility(View.VISIBLE);
        } else {
            emptyWishlistTv.setVisibility(View.GONE);
        }
        listView.setAdapter(opToursItemAdapter);
        listView.setDivider(null);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> listView, View view,
                                    int position, long id) {
                OpToursItemData itemClick = opToursItemAdapter.getItem(position);
                // Get the CITY name from this row in the database.
                String landServiceCode = itemClick.getLandServiceCode();
                startServiceOverViewActivity(landServiceCode);
            }
        });

        SwipeDismissListViewTouchListener touchListener =
                new SwipeDismissListViewTouchListener(
                        listView,
                        new SwipeDismissListViewTouchListener.DismissCallbacks() {
                            @Override
                            public boolean canDismiss(int position) {
                                return true;
                            }

                            public void onDismiss(ListView listView, int[] reverseSortedPositions) {
                                int position = reverseSortedPositions[0];
                                OpToursItemData opToursItemData = opToursItemAdapter.getItem(position);
                                String landServiceCode = opToursItemData.getLandServiceCode();
                                opToursItemAdapter.removeItem(position);
                                mDbHelper.updateWishList(landServiceCode, "N");
                                if (opToursItemAdapter.getCount() == 0){
                                    emptyWishlistTv.setVisibility(View.VISIBLE);
                                }
                            }
                        });

        listView.setOnTouchListener(touchListener);
        listView.setOnScrollListener(touchListener.makeScrollListener());
    }

    private void startServiceOverViewActivity(String landServiceCode) {
        Intent intent = new Intent(getActivity(), ServiceOverViewActivity.class);
        intent.putExtra(WISH_LIST_LAND_SERVICE_CODE, landServiceCode);
        startActivity(intent);
    }


    @Override
    public void onStop() {
        super.onStop();
        mDbHelper.close();
    }
}
